package com.yesep.ctrl;


import com.yesep.client.NettyClient;
import com.yesep.netty.NettyChannelManager;
import com.yesep.utils.CacheUtils;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @program: gateway
 * @description: 向指定客户端发送数据
 * @author: quliang
 * @create: 2019-07-09 14:08
 **/
@Controller
public class IndexController {

    @Autowired
    private NettyClient nettyClient;

    @ResponseBody
    @RequestMapping("/sendCmd")
    public boolean sendCmd(String mn, String cmdCode) {
        boolean flag = false;
        try {
            //nettyClient.start("",0);
//            nettySocketClient.send(mn, "aaaaa##");
            Channel channelByMn = NettyChannelManager.getInstance().getChannelByMn(mn);
            channelByMn.writeAndFlush("aaaaa\n\t");
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("/send")
    public ModelAndView send(ModelAndView mv, String mn) {
        mv.setViewName("send");
        System.out.println(mn);
        mv.addObject("mn", mn);
        return mv;
    }

    @RequestMapping("/index")
    public ModelAndView index(ModelAndView mv) {
        Map infos = CacheUtils.getInfos();
        System.out.println("infos = " + infos.size());
        mv.setViewName("index");
        mv.addObject("infos", infos);
        return mv;
    }
}
